﻿/*********************************************************
 * copyright learnren.com 版权所有
 * 开发人员：minyuan
 * 创建时间：2013/6/30 17:34:13
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.UI.LabelProviders
{
    public class NMLabel : AbstractLabelProvider
    {
        const string CHILDLISTFORMAT = "<a href=\"{0}\" target=\"_self\"><span>{1}</span></a>";

        /// <summary>
        /// 主导航标签 
        /// </summary>
        /// <param name="label"></param>
        public NMLabel(string label)
            : base(label)
        {

        }

        public override string GetHtml()
        {
            try
            {
                StringBuilder sbResult = new StringBuilder();
                sbResult.AppendLine("<ul class=\"menu\">");
                if (BasicCache.SiteConfig.IsStaticEnable)
                {
                    sbResult.AppendLine("<li class=\"current first\"><a href=\"" + System.Web.VirtualPathUtility.ToAbsolute("~/default.html") + "\" target=\"_self\">首页</a></li>");
                }
                else
                {
                    sbResult.AppendLine("<li class=\"current first\"><a href=\"" + System.Web.VirtualPathUtility.ToAbsolute("~/default.aspx") + "\" target=\"_self\">首页</a></li>");
                }
                var topCatalogues = from d in LEARNREN.COM.BLL.BasicCache.AllCatalogueList where d.ParentId == 0 orderby d.Order select d;
                int i = 0;
                int count = topCatalogues.Count();
                foreach (var tc in topCatalogues)
                {
                    if (i == count - 1)
                    {
                        sbResult.Append(getCatalogueMenuStr(tc, "last"));
                    }
                    else
                    {
                        sbResult.Append(getCatalogueMenuStr(tc));
                    }
                    i++;
                }
                sbResult.AppendLine("</ul>");
                return sbResult.ToString();
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                return this.LabelString;
            }
        }

        /// <summary>
        /// 获取栏目导航代码
        /// </summary>
        /// <param name="catalogue">栏目</param>
        /// <param name="classStr">css类</param>
        /// <returns></returns>
        private string getCatalogueMenuStr(Catalogue catalogue, string classStr = "")
        {
            StringBuilder sb = new StringBuilder();
            var children = from b in BasicCache.AllCatalogueList where b.ParentId == catalogue.Id orderby b.Order select b;
            int count = children.Count();
            if (count == 0)
            {
                if (string.IsNullOrEmpty(classStr))
                {
                    sb.Append("<li>");
                }
                else
                {
                    sb.Append("<li class=\"" + classStr + "\">");
                }
            }
            else
            {
                if (string.IsNullOrEmpty(classStr))
                {
                    sb.Append("<li class=\"li_p\">");
                }
                else
                {
                    sb.Append("<li class=\"li_p " + classStr + "\">");
                }
            }

            if (BasicCache.SiteConfig.IsStaticEnable)
            {
                sb.AppendLine(string.Format(CHILDLISTFORMAT, catalogue.HtmlPath, catalogue.Title));
            }
            else
            {
                sb.AppendLine(string.Format(CHILDLISTFORMAT,
                              System.Web.VirtualPathUtility.ToAbsolute(string.Format("~/{0}.aspx?id={1}", catalogue.CatalogueType, catalogue.Id)),
                              catalogue.Title));
            }

            if (count > 0)
            {
                sb.AppendLine("<dl class=\"li_p_content\">");
                sb.AppendLine("<dt></dt>");
                int i = 0;
                foreach (var cc in children)
                {
                    if (i != count - 1)
                    {
                        sb.Append(GetChildMenuStr(cc));
                    }
                    else
                    {
                        sb.Append(GetChildMenuStr(cc, "lastItem"));
                    }
                    i++;
                }
                sb.AppendLine("</dl>");
            }

            sb.AppendLine("</li>");
            return sb.ToString();
        }

        private string GetChildMenuStr(Catalogue catalogue, string classStr = "")
        {
            StringBuilder sb = new StringBuilder();

            // 获取子菜单
            var children = from d in BasicCache.AllCatalogueList where d.ParentId == catalogue.Id orderby d.Order select d;

            int count = children.Count();

            if (count > 0)
            {
                if (string.IsNullOrEmpty(classStr))
                {
                    sb.AppendLine("<dd class=\"dd_p\">");
                }
                else
                {
                    sb.AppendLine("<dd class=\"dd_p " + classStr + "\">");
                }
            }
            else
            {
                if (string.IsNullOrEmpty(classStr))
                {
                    sb.AppendLine("<dd>");
                }
                else
                {
                    sb.AppendLine("<dd class=\"" + classStr + "\">");
                }
            }

            if (BasicCache.SiteConfig.IsStaticEnable)
            {
                sb.AppendLine(string.Format(CHILDLISTFORMAT,
                                            catalogue.HtmlPath,
                                            catalogue.Title));
            }
            else
            {
                sb.AppendLine(string.Format(CHILDLISTFORMAT,
                                            System.Web.VirtualPathUtility.ToAbsolute(string.Format("~/{0}.aspx?id={1}", catalogue.CatalogueType, catalogue.Id)),
                                            catalogue.Title));
            }

            if (count > 0)
            {
                sb.AppendLine("<dl class=\"dd_p_content\">");
                sb.AppendLine("<dt></dt>");
                int i = 0;
                foreach (var cc in children)
                {
                    if (i != count - 1)
                    {
                        sb.AppendLine(GetChildMenuStr(cc));
                    }
                    else
                    {
                        sb.AppendLine(GetChildMenuStr(cc, "lastItem"));
                    }
                    i++;
                }
                sb.AppendLine("</dl>");
            }

            sb.AppendLine("</dd>");
            return sb.ToString();
        }
    }
}
